package com.nuatar.meibi;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.nuatar.meibi.pubvar.NetBus;
import com.nuatar.meibi.pubvar.PhoneMaster;
import com.nuatar.meibi.pubvar.WallPaper;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class Myservice extends Service {

	class MHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			if(msg.what>0){
				NotificationManager nm = (NotificationManager) getBaseContext()
						.getSystemService(Context.NOTIFICATION_SERVICE);
				Intent intent1 = new Intent(getBaseContext(), FirstActivity.class);
				PendingIntent pd = PendingIntent.getActivity(getBaseContext(), 0,
						intent1, Intent.FLAG_ACTIVITY_NEW_TASK);
				Notification nf = new Notification();
				nf.icon = R.drawable.ic_launcher;
				nf.tickerText = "美库有更新了！";
				nf.defaults = Notification.DEFAULT_SOUND;
				nf.defaults |= Notification.DEFAULT_VIBRATE;
				nf.defaults |= Notification.DEFAULT_LIGHTS;
				nf.flags = Notification.FLAG_AUTO_CANCEL;
				nf.setLatestEventInfo(getBaseContext(), "美库内容有更新",
						"更新了" + msg.what + "组图片", pd);
				nm.notify(110, nf);
			}
			stopSelf();
		}
	}
	private List<WallPaper> lps;
	public static Handler mhandler;

	@Override
	public void onCreate() {
		super.onCreate();
		mhandler = new MHandler();
	
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		MT mt = new MT(this.getApplicationContext());
		new Thread(mt).start();
		return START_NOT_STICKY;
	}

	@Override
	public void onDestroy() {
		this.stopSelf();
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	class MT implements Runnable {
		private Context context;

		public MT(Context ctx) {
			context = ctx;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			SharedPreferences pre = PreferenceManager
					.getDefaultSharedPreferences(context);
			String Keys = "MeikUseTime";
			String Time = pre.getString(Keys, "");
			boolean IsUp = pre.getBoolean("AutoUpdate", false);
			String datestr=pre.getString(Keys, "20130101000101");
			int coins=pre.getInt("MyCoins", 0);
			if (Time.equals("")) {
				
				Time = "10";
			}
			ConnectivityManager manager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo ninfo = manager.getActiveNetworkInfo();
			if(ninfo.isAvailable()){
				SharedPreferences.Editor edt = pre.edit();
				Date tm = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				String time = sdf.format(tm);
				try {//获取存储的时间戳信息
					Date th=sdf.parse(datestr);
					if(!PhoneMaster.DateCmp(tm, th)){
						coins+=5;
						edt.putInt("MyCoins", coins);
					}
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				edt.putString(Keys, time);
				edt.commit();
				Log.i("Time",Time+":"+time);
				lps = NetBus.getNewAt(Time);
				if (lps.size()>0) {
					Myservice.mhandler.sendEmptyMessage(lps.size());
					if (ninfo.getType() == ConnectivityManager.TYPE_WIFI && IsUp) {
						for (int i = 0; i < lps.size(); i++) {
							WallPaper wp = lps.get(i);
							NetBus.downloadDesFile(wp.id, null);
							NetBus.downloadLdtFile(wp.id);
							NetBus.downloadBdtFile(wp.id);
						}
					}

				}
			}
		}
	}
}
